package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_SmallGroup;
import com.kzd.sdk.ui.adapter.holder.UserSmallHolder;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_SmallAccountAdapter extends RecyclerView.Adapter<UserSmallHolder> {

    private List<KZD_SmallGroup> data = new ArrayList<>();
    private final Context mContext;
    private final int mType;

    private KZD_OnItemClickListener mOnItemClickListener;


    public KZD_SmallAccountAdapter(Context context, int type) {
        this.mContext = context;
        this.mType = type;
    }

    @NonNull
    @Override
    public UserSmallHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(AppUtils.getLayoutId(mContext, "kzd_item_small_account"), parent, false);
        return new UserSmallHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull UserSmallHolder holder, int position) {
        KZD_SmallGroup itemData = data.get(position);
        holder.tvSmallAccountHeader.setText(new StringBuilder("小号").append(position + 1));
        holder.tvSmallAccountName.setText(itemData.getUser_name());
        if (itemData.getLast_login() == 1) {
            holder.tvSmallAccountStatus.setVisibility(View.VISIBLE);
            if (mType == 1) {
                holder.tvSmallAccountStatus.setText("上次登录");
            } else {
                holder.tvSmallAccountStatus.setText("当前登录");
            }
        } else {
            holder.tvSmallAccountStatus.setVisibility(View.GONE);
            holder.tvSmallAccountStatus.setText(null);
        }
        if (mType == 1) {
            holder.tvSmallAccountAction.setText("进入游戏");
        } else {
            holder.tvSmallAccountAction.setText("切换");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener == null) {
                    return;
                }
                mOnItemClickListener.onClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_SmallGroup> smallResponseList) {
        if (smallResponseList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = smallResponseList;
        }
        notifyDataSetChanged();
    }

    public KZD_SmallGroup get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
